// packageK/business/index/index.js
const app = getApp()
const dayjs = require('../../lib/dayjs.min')
import { pie, bar } from '../../lib/chart-options'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    span: 12,
    total_withdraw_amount: '-',
    frozen_amount: '-',
    withdraw_able_amount: '-',
    chart1Options: bar,
    chart2Options: bar,
    chart3Options: pie
  },

  /**
   * 获取图表数据
   */
  getChartData: function(e) {
    const _this = this
    const { startDate, endDate} = e.detail
    const url = app.getNetAddresss('platformApplet.statistics.get-business')
    const data = {
      begin_time: dayjs(startDate).format('YYYY-MM-DD'),
      end_time: dayjs(endDate).format('YYYY-MM-DD')
    }
    wx.showLoading({
      title: '加载中...',
    })
    app._getNetWork({
      url,
      data,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          const { total_withdraw_amount, frozen_amount, withdraw_able_amount, supplier_list, primary_class_list, refund_primary_class_list } = resData.data
          let { chart1Options, chart2Options, chart3Options, chart4Options } = _this.data
          chart1Options.title.text = "销售总营业额"
          chart1Options.yAxis[0].data = supplier_list.map(item => item.name)
          chart1Options.series[0].data = supplier_list.map(item => item.amount)
          chart2Options.title.text = "产品类型销量额"
          chart2Options.yAxis[0].data = primary_class_list.map(item => item.name)
          chart2Options.series[0].data = primary_class_list.map(item => item.amount)
          chart3Options.title.text = "退货类型占比"
          chart3Options.series[0].data = refund_primary_class_list.map(item => {
            return {
              name: item.name,
              value: item.amount
            }
          })
          _this.setData({
            total_withdraw_amount: '¥' + total_withdraw_amount,
            frozen_amount: '¥' + frozen_amount,
            withdraw_able_amount: '¥' + withdraw_able_amount,
            chart1Options,
            chart2Options,
            chart3Options,
          })
        } else {
          app.tips(resData.msg)
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 提现
   */
  goWithdrawal: function() {
    wx.navigateTo({
      url: '/packageA/member/withdrawal/withdrawal',
    })
  },

  /**
   * 续费管理
   */
  goRenew: function() {
    wx.navigateTo({
      url: '/packageK/business/renew/renew',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})